package com.tianzeculture.stagebao.activity.mine;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.activity.business.NoticeDetailActivity;
import com.tianzeculture.stagebao.base.BaseFragment;
import com.tianzeculture.stagebao.bean.NoticeList;
import com.tianzeculture.stagebao.bean.OrderBean;
import com.tianzeculture.stagebao.bean.ResultBean;
import com.tianzeculture.stagebao.net.BusinessRestUsage;
import com.tianzeculture.stagebao.network.Request;
import com.tianzeculture.stagebao.network.enity.MSG;
import com.tianzeculture.stagebao.network.util.ToastUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.rong.imkit.RongIM;

import static com.tianzeculture.stagebao.util.Util.PAGE_SIZE;

/**
 * Created by Administrator on 2018/8/16.
 */

public class MyNoticeFrament extends BaseFragment {


    @BindView(R.id.my_recyclerview)
    RecyclerView mRecyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    @Request
    BusinessRestUsage businessRestUsage = null;
    private int toOrderId = 5;

    List<NoticeList> orderList = new ArrayList<>();
    private NoticeAdapter adapter;

    private int type;
    private int pager = 1;
    private int orderStatusId = 8;

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_my_notice;
    }

    public static MyNoticeFrament newInstance(int type) {
        Bundle args = new Bundle();
        args.putInt("type", type);
        MyNoticeFrament fragment = new MyNoticeFrament();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void setBundele(Bundle savedInstanceState) {
        super.setBundele(savedInstanceState);
        type = getArguments() == null ? 0 : getArguments().getInt("type");
    }

    @Override
    public void initUI() {
        super.initUI();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerview.setLayoutManager(layoutManager);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshlayout) {
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pager = 1;
                        if (type == 1) {
                            businessRestUsage.toAlreadyOrder(toOrderId, String.valueOf(type), pager, PAGE_SIZE, "0");
                        } else {
                            businessRestUsage.toAlreadyOrder(toOrderId, String.valueOf(type), pager, PAGE_SIZE, "1");
                        }
                        refreshlayout.finishRefresh();
                        refreshlayout.setNoMoreData(false);
                    }
                }, 2000);

            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pager++;
                        if (type == 1) {
                            businessRestUsage.toAlreadyOrder(toOrderId, String.valueOf(type), pager, PAGE_SIZE, "0");
                        } else {
                            businessRestUsage.toAlreadyOrder(toOrderId, String.valueOf(type), pager, PAGE_SIZE, "1");
                        }
                        refreshLayout.finishLoadMore();
                    }
                }, 2000);
            }
        });

        adapter = new NoticeAdapter(getActivity(), orderList);
        adapter.setOrderLisener(new NoticeAdapter.OrderLisener() {
            @Override
            public void contact(int position, NoticeList bean) {
                RongIM.getInstance().startPrivateChat(getActivity(), bean.getRid(), bean.getAddPeople());
                businessRestUsage.updateOrderStatus(orderStatusId, bean.getOrderId(), "1");
            }

            @Override
            public void toDetaile(int position, NoticeList bean) {
                Intent intent = new Intent(getActivity(), NoticeDetailActivity.class);
                intent.putExtra("notice", (Serializable) bean);
                intent.putExtra("isMy", true);
                startActivity(intent);
            }
        });
        mRecyclerview.setAdapter(adapter);

//        mRecyclerview.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
    }

    @Override
    public void onResume() {
        super.onResume();
        pager = 1;
        if (type == 1) {
            businessRestUsage.toAlreadyOrder(toOrderId, String.valueOf(type), pager, PAGE_SIZE, "0");
        } else {
            businessRestUsage.toAlreadyOrder(toOrderId, String.valueOf(type), pager, PAGE_SIZE, "1");
        }
    }


    @Override
    public void refreshUI(int taskId, MSG msg) {
        super.refreshUI(taskId, msg);
        if (taskId == toOrderId) {
            if (msg.getData()) {
                ResultBean resultBean = (ResultBean) msg.getObj();
                if (pager == 1)
                    orderList.clear();
                orderList.addAll(resultBean.getOrderlist());
                adapter.notifyDataSetChanged();
            }
        }
    }
}
